package com.data.csv;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.admin.bean.ActvOrDaka;
import com.base.bean.Activity;
import com.base.bean.ClientBase;
import com.base.bean.ClientPhone;
import com.base.bean.User;
import com.base.dao.ActivityDao;
import com.base.dao.ClientPhoneDao;
import com.base.domain.ActvtDetail;
import com.base.service.ActivityService;
import com.base.service.ClientService;
import com.base.service.UserService;

import tool.ClientTool;
import tool.JsonTool;
import tool.TimeTool;
import tool.encrypt.IdentifyTool;

/**
 * 
 * 临时导出数据
 * 导出行动数据
 * 
 * CSVUtils.exportCsv(new File(filePath), List<String>);
 * 
 * class QueryCondition{
 * 		Timestamp start;
		Timestamp end;
		String phone;
		Integer state,creator,cid,actvType;
	}
 * @author chz
 *
 */
public class ActvtCsvTool {
	
	public static void main(String[] args){
		String filepath = "/Users/chz/Desktop/csv-test.csv";
		
		ActvtCsvTool tool =  new ActvtCsvTool();
		String[] input = tool.data.split(",");
		
		ArrayList<ActvtDetail> result = new ArrayList<ActvtDetail>();
		
		for(String s :input){
			int cid = tool.getCidByPhone(s);
			
			if(cid==0)
				System.out.println(s+"--not exit");
			else{
				List<Activity> list = tool.getAllData(cid);
				List<ActvtDetail> result2 = tool.convertByActvtList(list);
				if(result2.size()>0){
					for(ActvtDetail data2:result2){
						result.add(data2);
					}
				}
				
				
			}
			
			
			
		}
		
		List<ActvOrDaka> finalData = tool.getFinal(result);
		
		// output
		tool.output(filepath, finalData);
		
		
	}
	
	
	
	static String data = "15229540777,15229544445,15009144961,18091408106," +
			"13991419831,18329988982,13033992277,13572373291,13892524055,15291320662," +
			"18991623456,13892625115,13772671099,13991758282,13399179798,18991763699,13509176881";
	
	
	static String[] actvType = { "", "空白市场开发", "二级渠道开发", "新客户拜访", "老客户拜访",
		"商务谈判", "交机", "备注其他", "客户催款","电话拜访","小挖开发","电话催收" };

	
	private Integer getCidByPhone(String phone){
		ClientPhoneDao dao = ClientService.getInstance().getClientPhoneDao();
		List<ClientPhone> list = dao.findByHql("from ClientPhone cp where cp.phone = '"+phone+"'");
		if(list.size()>0)
			return list.get(0).getCid();
		else
			return 0;
	}
	
	private List<Activity> getAllData(Integer cid){
		ActivityDao dao =  ActivityService.getInstance().getActvtDao();
		List<Activity> list = dao.findByHql("from Activity where cid="+cid);
		return list;
			
	}
	
	
	
	
	public List<Activity> getList(String phone){
		String hql = "from Activity where phone = :phone";
		Session session = ActivityService.getInstance().getActvtDao().getSessionFactory()
				.getCurrentSession();
		Transaction tx = session.beginTransaction();
		Query query = session.createQuery(hql).setString("phone", phone);

		List results = query.list();
		tx.commit();
		return results;
	}
	
	
	
	// 行动转化为ActvtDetial
	public List<ActvtDetail> convertByActvtList(List<Activity> acvts) {
			Map<Integer,User> usermap = null;
			
			Map<Integer,User> map = new HashMap();
			List<User> users = UserService.getInstance().getUserDao().findByHql("from User");
			for(User u:users){
				map.put(u.getUid(), u);
			}
			usermap=map;
			
			
			List<ActvtDetail> list = new ArrayList<ActvtDetail>();
			try {
				for (Activity a : acvts) {
					ActvtDetail ad = new ActvtDetail();
					ad.setActivity(a);
					try {
						ad.setUser(usermap.get(a.getCreator()));
//						ad.setUser(us
//								.getUserDao()
//								.findByHql(
//										"from User u where u.uid=" + a.getCreator())
//								.get(0));
						list.add(ad);
					} catch (Exception e) { // 未找到用户则跳过
						continue;
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
			return list;
		}
	
	
	public List<ActvOrDaka> getFinal(List<ActvtDetail> actvtDetailList){
		 List<ActvOrDaka> dataList = new ArrayList<ActvOrDaka>();

		

		if (actvtDetailList != null) {
			for (ActvtDetail ad : actvtDetailList) {
				ActvOrDaka data = new ActvOrDaka();
				data.setId(ad.getActivity().getAid());

				Integer _checked = ad.getActivity().getChecked();
				_checked = (_checked == null ? 0 : _checked);
				data.setState(_checked);
				data.setUid(ad.getUser().getUid());
				data.setType(2);
				data.setName(ad.getUser().getTruename());
				data.setDep(ad.getUser().getCompany());
				data.setProvince(ad.getUser().getProvince());
				data.setCity(ad.getUser().getCity());
				data.setAddress(ad.getActivity().getAddress());
				data.setPhoto(ad.getActivity().getPhotoName());
				data.setTime(ad.getActivity().getCreated());
				data.setPath(ad.getActivity().getPath());
				data.setActvType(ad.getActivity().getType()); // 行动类型
				
				
				// data.setClientCode("CN_"+new
				// Integer(ad.getActivity().getCid()*2+3).toString());
				data.setClientCode(IdentifyTool.enCodeCid(ad.getActivity()
						.getCid()));
				
				data.setClientName(ad.getActivity().getClientName());

				Integer it = ad.getActivity().getType();
				it = (it == null ? 0 : it);
				String content = ad.getActivity().getContent();
				content = (content == null ? "" : content);
				String _type = this.actvType[it];
				data.setMemo(content);

				if (ad.getActivity().getCheckmemo() != null)
					data.setCheckmemo(ad.getActivity().getCheckmemo());
				else
					data.setCheckmemo("");

				dataList.add(data);
			}
		}
		
		return dataList;
			
	}
	
	
	public void output(String filepath,List<ActvOrDaka> list){
		List<String> printData =  new ArrayList<String>();
		
		for(ActvOrDaka data:list){
			printData.add(data.toString());
		}
		
		tool.CSVUtils.exportCsv(new File(filepath), printData);
	}
	
	
}
